package msg.chestionar.ejb.beans;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import msg.chestionar.ejb.bi.RaspunsBI;

import com.msg.chestionar.entities.RaspunsuriE;
import com.msg.chestionar.to.ChestionarTO;
import com.msg.chestionar.to.IntrebareOptiuneTO;
import com.msg.chestionar.to.IntrebareTO;
import com.msg.chestionar.to.RaspunsuriTO;
import com.msg.chestionar.to.UtilizatorTO;

@Stateless
public class RaspunsBA implements RaspunsBI {

	@PersistenceContext
	private EntityManager em;


	public Integer getRaspunsuriNumber(long chestId, long intrebareId) {
		Integer result = 0;
		String sqlQuery = "select count(*) from T_RASPUNSURI r where r.chestionar_id = " + chestId + " and r.intrebare_id = " + intrebareId + " and r.comentariu is not null";
		Query query=em.createNativeQuery(sqlQuery);
		Vector v = (Vector) query.getSingleResult();
		if(v != null && !v.isEmpty()) {
			result = ((BigDecimal) v.get(0)).intValue();
		}
		return result;
	}
	
	public String getProcentaj(long chestId,long optId){
		Double result = 0d;
		if (this.getRaspunsuriByOption(chestId, optId) != 0){
			result = (double) this.getRaspunsuriByOption(chestId, optId) / this.getRaspNumberByChestionar(chestId);
			
			
			result *= 100;
			
			
			return result.toString() + "%";
		}
		else return "0%";
	}


	@Override
	public RaspunsuriTO addRaspuns(RaspunsuriTO r) {
		// TODO Auto-generated method stub
		return null;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<RaspunsuriTO> getAllRaspunsuri() {
		Query query = em.createNamedQuery("RaspunsuriE.findAll");

		return this.getRaspunsuriTOList(query.getResultList());
	}

	@Override
	public RaspunsuriTO getRaspunsById(long id) {
		Query query = em.createNamedQuery("RaspunsuriE.findById");
		query.setParameter("id", id);

		if (!query.getResultList().isEmpty()) {
			RaspunsuriE raspunsE = (RaspunsuriE) query.getResultList().get(0);
			return raspunsE.getRaspunsTO();
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<RaspunsuriTO> getRaspunsuriByOptiune(long id) {
		Query query = em.createNamedQuery("RaspunsuriE.getRaspByOptiune");
		query.setParameter("id", id);

		if (!query.getResultList().isEmpty()) {

			return getRaspunsuriTOList(query.getResultList());
		}
		return null;
	}

	private List<RaspunsuriTO> getRaspunsuriTOList(List<RaspunsuriE> raspE) {
		List<RaspunsuriTO> rezultat = new ArrayList<RaspunsuriTO>();
		for (RaspunsuriE rE : raspE) {
			rezultat.add(rE.getRaspunsTO());
		}
		return rezultat;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<RaspunsuriTO> getRaspunsuriByChestionar(long id) {
		
		
		Query query = em.createNamedQuery("RaspunsuriE.getRaspByChestionar");
		query.setParameter("id", id);
 
		if (!query.getResultList().isEmpty()) {

			return getRaspunsuriTOList(query.getResultList());
		}
		return null;
	}
	
		@SuppressWarnings("unchecked")
	@Override
	public List<RaspunsuriE> getRaspunsuriEByChestionar(long id) {
		
		
		Query query = em.createNamedQuery("RaspunsuriE.getRaspByChestionar");
		query.setParameter("id", id);
 
		if (!query.getResultList().isEmpty()) {

			return query.getResultList();
		}
		return null;
	}
	
	public Integer getRaspNumberByChestionar(long id){
		
		String sqlQuery = "select count(distinct r.user_id) from T_RASPUNSURI r where r.chestionar_id = " + id;
		Query q = em.createNativeQuery(sqlQuery);
		Integer nr = 0;
		
		Vector v = (Vector) q.getSingleResult();
		if(v != null && !v.isEmpty()) {
			nr = ((BigDecimal) v.get(0)).intValue();
		}
				
		return nr;
	}
	
	
	public Integer getRaspunsuriByOption(long chId, long optId){
		
		String sqlQuery = "select count(distinct r.user_id) from T_RASPUNSURI r where r.chestionar_id = " + chId + " and r.optiune_id = " + optId;
		Query q = em.createNativeQuery(sqlQuery);
		Integer n = 0;
		
		Vector v = (Vector) q.getSingleResult();
		if(v != null && !v.isEmpty()) {
			n = ((BigDecimal) v.get(0)).intValue();
		}

		return n;
	}

	
	
	@Override
	public List<RaspunsuriTO> getRaspText(long intrebId) {
		
		Query query = em.createNamedQuery("RaspunsuriE.getRaspText");
		query.setParameter("id", intrebId);


		return getRaspunsuriTOList(query.getResultList());
	}

	
	
	
	@SuppressWarnings("unchecked")
	@Override
	public List<RaspunsuriTO> getRaspunsuriByUser(long id) {
		Query query = em.createNamedQuery("RaspunsuriE.getRaspByUser");
		query.setParameter("id", id);

		if (!query.getResultList().isEmpty()) {

			return getRaspunsuriTOList(query.getResultList());
		}
		return null;
	}

	@Override
	public void saveRaspunsuri(ChestionarTO chestionar, UtilizatorTO user) {

		for (IntrebareTO intrebare : chestionar.getIntrebari()) {
			if (intrebare.getTip() == 1) {
				creazaRaspuns(chestionar, user, intrebare.getId(), intrebare
						.getRaspuns(), null);
			} else if (intrebare.getTip() == 2) {
				for (IntrebareOptiuneTO optiune : intrebare.getOptiuni()) {
					if (optiune.getSelectat()) {
						creazaRaspuns(chestionar, user, intrebare.getId(),
								intrebare.getRaspuns(), optiune.getId());
					}
				}
			} else if (intrebare.getTip() == 3) {
				creazaRaspuns(chestionar, user, intrebare.getId(), intrebare
						.getRaspuns(), intrebare.getRaspunsRadioButton());
			}
		}
	}

	private void creazaRaspuns(ChestionarTO chestionar, UtilizatorTO user,
			Long intrebareId, String raspuns, Long optiuneId) {
		RaspunsuriE entity = new RaspunsuriE();
		entity.setChestionar_id(chestionar.getId());
		entity.setComentariu(raspuns);
		entity.setIntrebare_id(intrebareId);
		entity.setUser_id(user.getId());
		entity.setOptiune_id(optiuneId);
		this.em.persist(entity);
	}


	@Override
	public void deleteRaspunsuriChestionar(long id) {
		List<RaspunsuriE> raspunsuriE= getRaspunsuriEByChestionar(id);
		for (RaspunsuriE raspunsE: raspunsuriE){
			this.em.remove(raspunsE);			
		}	
	}

}
